home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group96b.txt / 000052_icon-group-sender _Wed Nov 6 14:25:10 1996.msg < prev    next >
Internet Message Format  |  1997-01-02  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Wed, 6 Nov 1996 16:41:53 MST
  2. To: icon-group@cs.arizona.edu
  3. Date: Wed, 06 Nov 1996 14:25:10 -0700
  4. From: Steve Wampler <swampler@gemini.edu>
  5. Message-Id: <32810236.3855@gemini.edu>
  6. Organization: Gemini 8m Telescopes Project
  7. Sender: icon-group-request@cs.arizona.edu
  8. References: <55qhto$66r@netty.york.ac.uk>
  9. Subject: Re: sets and structures
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11.  
  12. stephen parker wrote:
  13. > i'm reading ``An Overview of the Icon Programming Language; Version 8''
  14. > (Griswold).  the section on sets says: ``insert(S, x) has no effect if x
  15. > is already in S''.
  16. > this appears to work for atomic types eg,
  17. ...
  18. > however, if if i try to insert a structure then it never fails,
  19. ...
  20. > as you would expect for a list rather than a set.  so, what's the answer
  21. > if you want to form a set of non-atomic types?  do i have to do it by
  22. > hand?
  23.  
  24.  
  25. Actually, both you and book are both correct.  The definition of a
  26. structured value is such that each such value is unique, even if
  27. they look the same.
  28.  
  29. There is no easy fix that works for arbitrary structured arguments.
  30. {A good exam question is "Why?"...}
  31.  
  32. For simple cases, such as the example you give, you could call
  33. a procedure that would produce a string that "looks like" the
  34. record with string-valued fields [note that image(rec(a,b)) does
  35. not work here], and then insert this string into the table.  Nor
  36. will any of the similar routines provided in the Icon Programming
  37. Language, since they all (image() and the others) tag each string
  38. representation for a structure with a unique id, just what you
  39. want to avoid...   {hmmm, good homework assignment...}
  40.  
  41. Personally, I would then use a table instead of a set.  Put the
  42. record itself as the value of an entry whose key is the "lookalike"
  43. string.  That way you can pull the record out from the 'set'...
  44.  
  45. Assuming that sImage(x) does this 'conversion' to string, and, using
  46. a table instead of a set, your code could be written as:
  47.  
  48.         procedure       main()
  49.                 local   l, s, r, a, b
  50.         
  51.                 s := table()      
  52.                 while(l := read()) do {
  53.                         a := l[1]
  54.                         b := l[2:0]
  55.                         
  56.             r := rec(a,b)
  57.             s[sImage(r)] := r
  58.                 }
  59.                 every l := !s do
  60.                         write(l.a, " ", l.b)
  61.         end
  62.  
  63. For this simple case, here is a suitable sImage():
  64.  
  65.     procedure    sImage(x)
  66.         return x.a||" "||x.b
  67.     end
  68.  
  69. It's generalizing this that is hard...
  70. -- 
  71.  
  72. Steve Wampler - swampler@gemini.edu [Gemini 8m Telescopes Project (under
  73. AURA)]
  74. The Gods that smiled upon your birth are laughing now. -- fortune cookie
  75.